// cartApi.js
import api from './api';
// 添加到购物车
export const addToCart = async (cartItem) => {
    try {
        const response = await api.post('/api/cart/add', cartItem);
        return response.data;
    } catch (error) {
        console.error('addToCart error:', error);
        throw error;
    }
};
// 获取购物车项目
export const getCartItems = async (userId) => {
    try {
        const response = await api.get(`/api/cart/${userId}`);
        return response.data;
    } catch (error) {
        console.error('getCartItems error:', error);
        throw error;
    }
};
// 更新购物车项目数量
export const updateCartItemQuantity = async (userId, drugId, quantity) => {
    try {
        await api.post(`/api/cart/update`, {userId, drugId, quantity});
    } catch (error) {
        console.error('updateCartItemQuantity error:', error);
        throw error;
    }
};
// 删除购物车项目
export const removeCartItem = async (userId, drugId) => {
    try {
        await api.delete(`/api/cart/remove/${userId}/${drugId}`);
    } catch (error) {
        console.error('删除购物车项目错误：', error);
        throw error;
    }
};

// 清空购物车
export const clearCart = async (userId) => {
    try {
        const response = await api.delete(`/api/cart/clear/${userId}`);
        return response.data;
    } catch (error) {
        console.error('清空购物车错误：', error);
        throw error;
    }
};

